home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.08 Aug 91 / Browser sources / CBrowserDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-19  |  5.1 KB  |  213 lines  |  [TEXT/KAHL]

  1. /******************************************************/
  2. /*                                                                                                      */
  3. /*    CBrowserDoc.c                                                                          */
  4. /*                                                                                                      */
  5. /*    Written in Think C version 4.0.2                                  */
  6. /*    Based on CStarterDoc.c                                                      */
  7. /*                                                                                                      */
  8. /*    Allen Stenger    January 1991                                              */
  9. /*                                                                                                      */
  10. /******************************************************/
  11.  
  12. #define    DOTIMING    0            
  13.     /* do timing measurements if true */
  14.  
  15. #include <Global.h>
  16. #include <Commands.h>
  17. #include <CApplication.h>
  18. #include <CBartender.h>
  19. #include <CDataFile.h>
  20. #include <CDecorator.h>
  21. #include <CDesktop.h>
  22. #include <CError.h>
  23. #include <CPanorama.h>
  24. #include <CScrollPane.h>
  25.  
  26. #include "CBrowserDoc.h"
  27. #include "CBrowserPane.h"
  28. #include "Browser.h"
  29. #include "Search.h"
  30. #include "LineCount.h"
  31.  
  32. extern    CApplication    *gApplication;
  33. extern    CBartender        *gBartender;
  34. extern    CDecorator        *gDecorator;
  35. extern    CDesktop            *gDesktop;
  36. extern    CBureaucrat        *gGopher;
  37. extern    OSType                gSignature;
  38. extern    CError                *gError;
  39.  
  40. void CBrowserDoc::IBrowserDoc(
  41.                 CApplication *aSupervisor, 
  42.                 Boolean printable)
  43. {
  44.     CDocument::IDocument(aSupervisor, printable);
  45.     itsDataH = NULL;
  46.     itsLineStartsH = NULL;
  47.     itsLineCt = 0;
  48.     itsFindOffset = -1;
  49. }
  50.  
  51. void CBrowserDoc::Dispose()
  52. {
  53.     if (itsDataH != NULL) DisposHandle(itsDataH);
  54.     if (itsLineStartsH != NULL) 
  55.         DisposHandle(itsLineStartsH);
  56.     if (itsMainPane != NULL) itsMainPane->Dispose();    
  57.     inherited::Dispose();
  58. }
  59.  
  60. void CBrowserDoc::FindIt(long offset)
  61. {
  62.     Point    scrollToPoint;        
  63.         /* point in panorama coordinates to scroll to            */
  64.     int        i;        /* loop control                                                 */
  65.     
  66.     #if DOTIMING
  67.     long    startTime, endTime; 
  68.     float    elapsedTime;    
  69.         /* time to search and scroll to right line (secs)    */
  70.         
  71.     long    startLineTime, endLineTime;
  72.     float    elapsedLineTime;    
  73.         /* time to find right line given the offset (secs)*/
  74.     #endif
  75.  
  76.     #if DOTIMING
  77.             startTime = TickCount();
  78.     #endif
  79.         
  80.         if ( GoFind(itsDataH, &offset) ) {
  81.             #if DOTIMING
  82.                 startLineTime = TickCount();
  83.             #endif
  84.             for (i=1;
  85.                 (*itsLineStartsH)[i]<=offset;
  86.                 i++)
  87.                 ;
  88.             #if DOTIMING
  89.                 endLineTime = TickCount();
  90.                 elapsedLineTime = (endLineTime - startLineTime) 
  91.                     / 60.0;
  92.             #endif
  93.             scrollToPoint.v = i - 1;
  94.             scrollToPoint.h = 0;
  95.             ((CBrowserPane *)itsMainPane)->
  96.                 ScrollTo(scrollToPoint, TRUE);
  97.             itsFindOffset = offset;
  98.             }
  99.         else SysBeep(1); /* not found */
  100.         
  101.         #if DOTIMING
  102.             endTime = TickCount();
  103.             elapsedTime = (endTime - startTime) / 60.0;
  104.         #endif
  105. }
  106.                                             
  107. void CBrowserDoc::DoCommand(long theCommand)
  108. {
  109.     Boolean aSearchStringGiven;    
  110.         /* Find dialog got string                                                 */    
  111.  
  112.     switch (theCommand) {
  113.  
  114.         case cmdFind: 
  115.                     aSearchStringGiven = GetSearchString();
  116.                     itsWindow->Update();     
  117.                         /* update window to correct damage                 */
  118.                         /* from Find dialog                                             */
  119.                     if (aSearchStringGiven) {
  120.                         itsFindOffset = -1;
  121.                         FindIt( itsFindOffset + 1 );
  122.                         }
  123.                     break;
  124.                     
  125.         case cmdFindAgain: 
  126.                     FindIt( itsFindOffset + 1 );
  127.                     break;
  128.                     
  129.         default:    inherited::DoCommand(theCommand);
  130.                     break;
  131.     }
  132. }
  133.  
  134. void CBrowserDoc::UpdateMenus()
  135. {
  136.     inherited::UpdateMenus();
  137.  
  138.     gBartender->EnableCmd(cmdFind);  
  139.     if ( HaveSearchString() ) 
  140.         gBartender->EnableCmd(cmdFindAgain);  
  141.     gBartender->DisableCmd(cmdNew);
  142.     gBartender->DisableCmd(cmdSaveAs);
  143. }
  144.  
  145. void CBrowserDoc::OpenFile(SFReply *macSFReply)
  146. {
  147.     CDataFile    *theFile;
  148.     Handle        theData;
  149.     Str63            theName;
  150.     OSErr            theError;
  151.     short            theLineCt;                /* shadow variable             */
  152.     long            **theLineStartsH; /* shadow variable             */
  153.     
  154.     theFile = new(CDataFile);
  155.     theFile->IDataFile();
  156.     theFile->SFSpecify(macSFReply);
  157.     itsFile = theFile;
  158.     theError = theFile->Open(fsRdPerm);
  159.     if (!gError->CheckOSError(theError)) {
  160.         Dispose();
  161.         return;
  162.     }
  163.   gApplication->RequestMemory(FALSE, TRUE);
  164.   theFile->ReadAll(&theData);     
  165.   
  166.   /* Reset canFail to FALSE for default                             */
  167.   /* memory-error handling.                                                     */
  168.   gApplication->RequestMemory(FALSE, FALSE);
  169.     if (theData == NULL) {
  170.         gError->CheckOSError(MemError());
  171.         Dispose();
  172.         return;
  173.     }
  174.     itsDataH = theData;        
  175.         /* save handle to data in instance variable             */
  176.     DoLineCount( itsDataH, &theLineCt, &theLineStartsH ); 
  177.     itsLineCt = theLineCt;
  178.     itsLineStartsH = theLineStartsH;
  179.     BuildWindow(theData);
  180.     itsFile->GetName(theName);
  181.     itsWindow->SetTitle(theName);
  182.     itsWindow->Select();
  183.     theError = itsFile->Close();
  184.     itsFile = NULL;
  185. }
  186.  
  187. void CBrowserDoc::BuildWindow (Handle theData)
  188. {
  189.     CScrollPane        *theScrollPane;
  190.     CBrowserPane    *theMainPane;
  191.         
  192.     itsWindow = new(CWindow);
  193.     itsWindow->IWindow(WINDBrowser, FALSE, 
  194.                                             gDesktop, this);
  195.   theScrollPane = new(CScrollPane);
  196.   theScrollPane->IScrollPane(itsWindow, this, 
  197.                               10, 10, 0, 0,
  198.                                 sizELASTIC, sizELASTIC,
  199.                                 TRUE, TRUE, TRUE);
  200.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  201.     theMainPane = new(CBrowserPane);
  202.     theMainPane->IBrowserPane(theScrollPane, this, 
  203.                     0, 0, 0, 0, 
  204.                     sizELASTIC, sizELASTIC, 
  205.                     itsDataH, itsLineCt, itsLineStartsH);
  206.     itsMainPane = theMainPane;
  207.     itsGopher = theMainPane;
  208.     theMainPane->FitToEnclosure(TRUE, TRUE);
  209.     theScrollPane->InstallPanorama(theMainPane);
  210.     gDecorator->PlaceNewWindow(itsWindow);
  211.     itsWindow->Zoom(inZoomOut); /* open full-screen            */
  212. }
  213.